package demo;

import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

public class XMLParse {
    public static void main(String[] args) throws IOException, DocumentException {
    testAsXml2();
    }

    public static void testAsXml2() throws DocumentException {
        SAXReader reader=new SAXReader();
        Document document = reader.read("ch02/src/book.xml");
        //document的asXml方法可以把xml文件转成字符串
        String s = document.asXML();
        System.out.println(s);
        //把字符串转成xml文件格式
        DocumentHelper.parseText(s);
    }
    public static void select2(){
        SAXReader reader=new SAXReader();
        try {
            Document document = reader.read("ch02/src/book.xml");
            Element rootElement = document.getRootElement();
            List<Element> book = rootElement.elements("book");
            for (Element element : book) {
                System.out.println("=====================");
                System.out.println("标签名："+element.getName());
                System.out.println("id的属性值"+element.attributeValue("id"));
                Element title = element.element("title");
                Element author = element.element("author");
                Element description = element.element("description");
                //获取文本
                System.out.println("title标签中间的文本："+title.getText());
                System.out.println("author标签中间的文本："+author.getText());
                System.out.println("description标签中间的文本："+description.getText());
            }



        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }


    public static void select(){
        Document document= null;
        try {
            SAXReader sr=new SAXReader();
            document = sr.read("ch02/src/book.xml");
            Element root=document.getRootElement();
            List<Element> list=root.elements("book");
            for (Element element : list) {
                System.out.println("=====================");
                System.out.println("标签名："+element.getName());
                System.out.println("id的属性值"+element.attributeValue("id"));
                Element titleElement=element.element("title");
                Element authorElement=element.element("author");
                Element descriptionElement=element.element("description");

                //获取文本
                System.out.println("title标签中间的文本："+titleElement.getText());
                System.out.println("author标签中间的文本："+authorElement.getText());
                System.out.println("description标签中间的文本："+descriptionElement.getText());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }
    public static  void add2() throws DocumentException, IOException {
        SAXReader sr=new SAXReader();
        Document read = sr.read("ch02/src/book.xml");
        Element root=read.getRootElement();
        List<Element> list = root.elements();
        Element book = list.get(1);
        Element lllll = book.addElement("lllll");
        lllll.setText("5555.33");
        XMLWriter writer=new XMLWriter(new FileWriter("ch02/src/book.xml"));
        writer.write(root);
        writer.flush();
        writer.close();

    }
    public static void add() {
        XMLWriter writer = null;

        try {
            SAXReader sr = new SAXReader();
            Document document = sr.read("ch02/src/book.xml");
            Element root = document.getRootElement();
            List<Element> list = root.elements();
            Element book = list.get(1);
            Element introduce = book.addElement("price");
            introduce.setText("55.55");
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            writer = new XMLWriter(new FileWriter("ch02/src/book.xml"), format);
            writer.write(document);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void update2() throws DocumentException, IOException {

        SAXReader reader=new SAXReader();
        Document document = reader.read("ch02/src/book.xml");
        Element rootElement = document.getRootElement();
        List<Element> book = rootElement.elements("book");
        Element element = book.get(1);
        Element author = element.element("author");
        author.setText("约吗");
        OutputFormat format=new OutputFormat();
        format.setEncoding("utf-8");
        XMLWriter writer=new XMLWriter(new FileWriter("ch02/src/book.xml"),format);
        writer.write(document);
        writer.flush();
        writer.close();
    }
    public static void update() {
        XMLWriter writer = null;
        try {
            //1.创建解析器
            SAXReader sr=new SAXReader();
            //2.解析指定地址的xml文件
            Document document = sr.read("ch02/src/book.xml");
            //3.获取文档根节点
            Element root=document.getRootElement();
            //4.通过根节点获取子节点
            List<Element> list = root.elements();
            //修改第二个book标签的子标签author的值为岳不群
            Element book = list.get(1);
            //5.通过book找到他的子节点author
            Element author=book.element("author");
            author.setText("岳不群");
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            writer = new XMLWriter(new FileWriter("ch02/src/book.xml"), format);
            writer.write(document);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void delete2() throws DocumentException, IOException {
        SAXReader reader=new SAXReader();
        Document document = reader.read("ch02/src/book.xml");
        Element rott = document.getRootElement();
        List<Element> list = rott.elements("book");
        Element element = list.get(1);
        Element price = element.element("price");
        list.remove(price);
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter writer = new XMLWriter(new FileWriter("ch02/src/book.xml"), format);
        writer.write(document);
        writer.flush();
        writer.close();
    }
    public static void delete(){
        XMLWriter writer=null;

        try {
            SAXReader sr=new SAXReader();
            Document document=sr.read("ch02/src/book.xml");
            Element root = document.getRootElement();
            List<Element> list = root.elements("phone");

            Element book = list.get(1);
            Element price = book.element("price");
            book.remove(price);
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("utf-8");
            writer = new XMLWriter(new FileWriter("ch02/src/book.xml"), format);
            writer.write(document);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void selectXpath() throws DocumentException {
        SAXReader sr=new SAXReader();
        Document document = sr.read("ch02/src/book.xml");
        Element rootElement = document.getRootElement();
//        List<Node> list = rootElement.selectNodes("//author");
//        Node node = list.get(0);
        List<Node> list = rootElement.selectNodes("/books/book/author");
        Node node = list.get(0);
        System.out.println(node.getText());

    }
    public static void selectByXpath(){
        SAXReader sr=new SAXReader();
        try {
            Document document = sr.read("ch02/src/book.xml");
            //3.使用xpath语法
            //绝对路劲
            Node author=(Node) document.selectNodes("/books/book/author").get(0);
            //相对路劲
            //Node author=(Node) document.selectNodes("//author").get(0);
            //4.取节点的文本
            System.out.println(author.getText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void testAsXml() {
        //1.获取解析器
        SAXReader sr=new SAXReader();
            //2.解析指定路劲的xml文件，得到document对象
        try {
            Document document = sr.read("ch02/src/book.xml");
            //xml转字符串
            System.out.println(document.asXML());
                //字符串转xml
            Document document2= DocumentHelper.parseText(document.asXML());
            System.out.println(document2.getRootElement());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
